package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import com.lzx.iteam.bean.MyTalkMessageData;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Megan on 15/10/16.
 */
public class GetMyTalkMessageMsg extends CcMsgStructure {


    private Message mmCallback;
    private Context mContext;
    private CloudDBOperation cloudDBOperation;

    public GetMyTalkMessageMsg(Message callback, Context context) {
        mmCallback = callback;
        mContext = context;
        cloudDBOperation = new CloudDBOperation(mContext);
    }

    @Override
    public void onReceive(JSONObject data) {
        ArrayList<MyTalkMessageData> talkListDatas = new ArrayList<MyTalkMessageData>();
        try {
            JSONArray jsonArray = data.getJSONArray(AsynHttpClient.KEY_CC_DATA);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                MyTalkMessageData messageData = new MyTalkMessageData();
                messageData.msgUName = jsonObject.getString("msg_uname");
                if (jsonObject.has("msg_comment")) {
                    messageData.msgComment = jsonObject.getString("msg_comment");
                }
                messageData.msgType = jsonObject.getString("msg_type");
                messageData.msgUImg = jsonObject.getString("msg_uimg");
                messageData.msgUId = jsonObject.getString("msg_uid");
                messageData.msgCTime = jsonObject.getString("msg_ctime");
                messageData.talkId = jsonObject.getString("talk_id");
                if (jsonObject.has("talk_thumb")) {
                    messageData.talkThumb = jsonObject.getString("talk_thumb");
                }
                messageData.talkUId = jsonObject.getString("talk_uid");
                messageData.talkUName = jsonObject.getString("talk_uname");
                messageData.talkUImage = jsonObject.getString("talk_uimg");
                JSONObject jsonObject1 = jsonObject.getJSONObject("talk");
                if (jsonObject1.has("text")) {
                    messageData.text = jsonObject1.getString("text");
                }
                talkListDatas.add(messageData);
                cloudDBOperation.insertTalkMyMsgData(messageData);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


        if (mmCallback != null) {
            mmCallback.obj = talkListDatas;
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mmCallback.obj = errorMsg;
            } else {
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }
    }
}